const { Controller } = require("egg");

class ShopController extends Controller {
  async getShop() {
    const { ctx } = this;
    const data = await ctx.app.mysql.select("shop");

    ctx.body = {
      code: 1,
      message: "获取列表成功",
      data,
    };
  }

  async addShop() {
    const { ctx } = this;
    const userInfo = ctx.request.body;
    const data = await ctx.app.mysql.insert("shop", userInfo);
    if (data.affectedRows === 1) {
      ctx.body = {
        code: 1,
        message: "新增成功",
      };
    }
  }

  // 删除接口
  async delShop() {
    const { ctx } = this;
    const id = ctx.query.id;
    const data = await ctx.app.mysql.delete("shop", {
      id,
    });
    if (data.affectedRows === 1) {
      ctx.body = {
        code: 1,
        message: "删除成功",
      };
    }
  }

  // 编辑接口
  async editShop() {
    const { ctx } = this;
    const editInfo = ctx.request.body;
    const data = await ctx.app.mysql.update("shop", editInfo);
    if (data.affectedRows === 1) {
      ctx.body = {
        code: 1,
        message: "编辑成功",
      };
    }
  }

  async shopSearch() {
    const { ctx } = this;
    const value = ctx.query.title; // 从查询参数中获取搜索关键词
    // 假设这里是直接在 controller 中进行模糊搜索订单表
    const list = await this.app.mysql.query(`SELECT * FROM shop WHERE shopName LIKE '%${value}%'`)
    ctx.body = {
      code: 1,
      message: "搜索成功",
      list
    };
  }
}

module.exports = ShopController;
